package com.car.mgr.web.qdsl.base;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.io.Serializable;
import java.lang.SuppressWarnings;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.transaction.annotation.Transactional;

public class BaseServiceImpl<T extends BaseEntity<ID>, ID extends Serializable, DAO extends JpaRepository<T, ID> & QuerydslPredicateExecutor<T>> {
  @Autowired
  protected DAO dao;

  @Autowired
  protected JPAQueryFactory jpaQueryFactory;

  public BaseServiceImpl() {
  }

  @Transactional
  @Modifying
  public T save(T val) {
    val = dao.save(val);
    return val;
  }

  @SuppressWarnings("unchecked")
  public T findById(ID id) {
    return dao.findById(id).orElse(null);
  }

  @Transactional
  @Modifying
  public void deleteById(ID id) {
    dao.deleteById(id);
  }

  public List<T> findAll() {
    return dao.findAll();
  }

  public Page<T> findAll(Pageable page) {
    return dao.findAll(page);
  }
}
